﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DYP
{
    static class EnumerableExtensions
    {
        public static String AsJoined(this IEnumerable<String> enumerable)
        {
            return AsJoined(enumerable, ",");
        }

        public static String AsJoined(this IEnumerable<String> enumerable, String separator)
        {
            return String.Join(separator, enumerable.ToArray());
        }
    }
    class qd
    {
        public string qdm;
        public string hz;
        public string pyin;

        public qd(string _qdm, string _hz, string _pyin)
        {
            this.qdm = _qdm;
            this.hz = _hz;
            this.pyin = _pyin;
        }

        public void output()
        {

            System.Console.WriteLine(this.qdm + "," + this.hz + "," + this.pyin);
        }
    }
    class pyin_hzs
    {
        public string hzs;
        public string pyin;

        public pyin_hzs(string _hzs, string _pyin)
        {

            this.hzs = _hzs;
            this.pyin = _pyin;
        }

        public void output()
        {

            System.Console.WriteLine(this.hzs + "," + this.pyin);
        }
    }
    class hz_pyin
    {
        public string hz;
        public string pyin;

        public hz_pyin( string _hz, string _pyin)
        {

            this.hz = _hz;
            this.pyin = _pyin;
        }

        public void output()
        {

            System.Console.WriteLine(this.hz + "," + this.pyin);
        }
    }

    //========================================================================================================
    class Program
    {
        public List<hz_pyin> table1=new List<hz_pyin>();
        public List<hz_pyin> table2=new List<hz_pyin>();
        //========================================================================================================
        public void read1() {
            List<qd> table1 = new List<qd>();
            string line;
            System.IO.StreamReader file = new System.IO.StreamReader("./DYP5.txt");
            while ((line = file.ReadLine()) != null)
            {
                string[] result = line.Split();
                var qdm = result[0];
                var hz = result[1];
                var pyin = result[2];
                qd row = new qd(qdm, hz, pyin);
                table1.Add(row);

            }
            var table2 = from pp in table1
                         group pp by pp.hz into gg
                         select new
                         {
                             hz = gg.Key,
                             pyin = gg.Select(g => g.pyin).Distinct().AsJoined(",")
                         };

            foreach (var item in table2)
            {
                hz_pyin hp = new hz_pyin(item.hz,item.pyin);
                //将结果放入全局表中去
                this.table1.Add(hp);
                //System.Console.WriteLine(item.hz + " " + item.pyin);
            }

            file.Close();

            // Suspend the screen.
            // Console.ReadLine();
        }
        //========================================================================================================
        /// <summary>
        /// 
        /// </summary>
        public void read2()
        {
            List<pyin_hzs> table1 = new List<pyin_hzs>();
            List<hz_pyin> table3 = new List<hz_pyin>();
            string line;
            System.IO.StreamReader file = new System.IO.StreamReader("./gbutf.txt");
            while ((line = file.ReadLine()) != null)
            {
                string[] result = line.Split();
                var pyin = result[0].Remove(result[0].Length-1);
                var hzs = (result.Length==2)?result[1]:null;
                pyin_hzs row = new pyin_hzs(hzs, pyin);
                table1.Add(row);

            }
            //将拼音多音字等GOURP，最后成 拼音:汉子串的形式
            var table2 = from pp in table1
                         group pp by pp.pyin into gg
                         select new
                         {
                             pyin = gg.Key,
                             hzs = gg.Select(g => g.hzs).Distinct().AsJoined("")
                         };
            //将汉子串打散，成为 汉字：拼音形式
            foreach (var item in table2)
            {
                char[] hzs = item.hzs.ToCharArray();
                foreach (var hz in hzs) {
                    hz_pyin hp = new hz_pyin(hz.ToString(),item.pyin);
                    table3.Add(hp);
                }
            }

            var table4 = from pp in table3
                         group pp by pp.hz into gg
                         select new
                         {
                             hz=gg.Key,
                             pyin=gg.Select(g=>g.pyin).Distinct().AsJoined(",")
                         };
            foreach (var item in table4) {
                hz_pyin hp = new hz_pyin(item.hz, item.pyin);
                this.table2.Add(hp);
            }

            file.Close();

            // Suspend the screen.
            // Console.ReadLine();
        }

        public Program() {
            read1();
            read2();
            var result = from pp in this.table1
                     from qq in this.table2
                     where pp.hz == qq.hz
                     select new
                     {
                         hz = pp.hz,
                         pyin1 = pp.pyin,
                         pyin2 = qq.pyin
                     };
            foreach(var item in result){
                System.Console.WriteLine(item.hz+" : "+item.pyin1+"  "+item.pyin2);
            }

        }
        static void Main(string[] args)
        {
            Program a = new Program();

        }
    }
}
